<!DOCTYPE HTML>
<html  xmlns:th="http://www.thymeleaf.org">
<head th:include="_meta :: header">
    <title>添加</title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form form form-horizontal"  id="form-draft-add" action="#" th:action="@{draft/writer/toDraftBox}">
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>稿件标题：</label>
                <div class="layui-input-inline">
                    <input type="layui-input" class="layui-input" t name="draftTitle"  placeholder="稿件标题" id="draftTitle" style="width:400px;"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>文件名：</label>
                <div class="layui-input-inline">
                    <input type="layui-input" class="layui-input" t name="fileName"  placeholder="文件名" id="fileName" style="width:400px;"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>稿件内容：</label>
                <div class="layui-input-block" >
                    <script id="editor" name="draftText"  type="text/plain"  style="height:500px;">
                    </script>
                </div>
            </div>

            <input type="hidden" t name="draftNum"  placeholder="稿件字数" id="draftNum"/>
            <input type="hidden" value="2" t name="statusCode"  placeholder="状态code" id="statusCode"/>
            <input type="hidden" id="action" name="action" value="">

            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>稿件类型</label>
                <div class="layui-input-inline">
                <div th:if="${roleId == 6}">
                    <input name="draftType" type="radio" th:if="${c.key == 1}"
                           th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" checked/>
                    <input name="draftType" type="radio" th:if="${c.key != 1 && c.key < 5}"
                           th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" /><br/>
                </div>
<!--                    6队流程 文字稿件/视频稿件   -->
                <div th:if="${roleId == 40}">
                    <input  name="draftType" type="radio" th:if="${c.key == 5}"
                            th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" checked /><br/>
                    <input  name="draftType" type="radio" th:if="${c.key > 5 && c.key < 7}"
                           th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" /><br/>
                </div>
<!--                    7队流程-->
                <div th:if="${roleId == 46}">
                    <input  name="draftType" type="radio" th:if="${c.key == 7}"
                            th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" checked/><br/>
                    <input  name="draftType" type="radio" th:if="${c.key > 7}"
                            th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}" /><br/>
                </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" id="submit" type="submit" onclick="submitFun('submit');">
                    发布
                </button>
                <button  class="layui-btn" id="save" type="submit" onclick="submitFun('save');">
                    存稿
                </button>
            </div>
        </form>
    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<div th:replace="_footer :: footerjs"></div>
<div th:replace="_ueditor :: ueditor"></div>
<!--/_footer 作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script>
    layui.use(['form', 'layer','laydate'],function() {
        $ = layui.jquery;
        var form = layui.form,
                laydate = layui.laydate,
                layer = layui.layer;
        lay('.timeSpace').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });
    });
    function submitFun(act)
    {
        $('#action').val(act);
    };
    $(function(){
        $("#form-draft-add").validate({
            rules:{
                draftTitle:{
                    required: true,
                },
                draftText:{
                    required: true,
                },
                draftNum:{
                    required: true,
                },
                draftType:{
                    required: true,
                },
                fileName:{
                    required:true,
                },
            },
            onkeyup:false,
            debug: true,
            success:"valid",
            submitHandler:function(form){
                var draftNum = UE.getEditor('editor').getContentTxt().length;
                $("#draftNum").val(draftNum);
                var draftType = $('input:radio[name="draftType"]:checked').val()
                if(draftType == 7) {
                    if(draftNum>400) {
                        selfmsg("请确认稿件数字在400字以内或选择其他稿件类型");
                        return false;
                    }
                }else if(draftType == 8){
                    if(draftNum < 400 || draftNum > 1000){
                        selfmsg("请确认稿件数字在400字到1000字或选择其他稿件类型");
                        return false;
                    }
                }else if(draftType == 9){
                    if(draftNum < 1000){
                        selfmsg("请确认稿件数字在1000字以上或选择其他稿件类型");
                        return false;
                    }
                }
                $.lock();
                if($('#action').val() == 'submit'){
                    var url = "/draft/writer/add";
                    $(form).ajaxSubmit({
                        type: 'POST',
                        url: url ,
                        success: function(data){
                            if(data.code == "1"){
                                selfmsg('添加成功!',1,function(){
                                    location.reload();
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.location.reload();
                                    parent.layer.close(index);
                                    // window.location.href = "/draft/writer?statusCode=1001";
                                });
                            }else{
                                selfmsg('添加失败!');
                                $.unlock();
                            }
                        },
                        error: function(){
                            $.unlock();
                            selfmsg('添加异常!',3);
                        }
                    });
                }else if($('#action').val() == 'save'){
                    var url = "/draft/writer/add";
                    $(form).ajaxSubmit({
                        type: 'POST',
                        url: url ,
                        success: function(data){
                            if(data.code == "1"){
                                selfmsg('保存成功!',1,function(){
                                    var index =layer.getFrameIndex(window.name);
                                    // parent.location.reload();
                                    // parent.layer.close(index);
                                    // layer.close(window);
                                    window.location.href = "/draft/writer/draftBox";
                                });
                            }else{
                                selfmsg('保存失败!');
                                $.unlock();
                            }
                        },
                        error: function(){
                            $.unlock();
                            selfmsg('保存异常!',3);
                        }
                    });
                }
                }

        });
    });

</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>